Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
N+1問題が起きる原因について調べてみた
Search
smallmonkeykey
January 19, 2024
51
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
N+1問題が起きる原因について調べてみた
学習中の者です。
1月11日のOmotesando.rbで、初めてのLTをしました。
smallmonkeykey
January 19, 2024
More Decks by smallmonkeykey
See All by smallmonkeykey
投票アプリの反省と新機能
smallmonkeykey
0
65
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
910
Into the Great Unknown - MozCon
thekraken
41
2.8k
Writing Fast Ruby
sferik
630
63k
Thoughts on Productivity
jonyablonski
76
5.4k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
570
Building an army of robots
kneath
307
47k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Navigating Weather and Climate Data
rabernat
0
550
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
540
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Transcript
N+1問題が起きる原因に ついて調べてみた 2024/1/11
自己紹介 リサ ・現在fjordbootcampで学習中 ・好きな生き物は犬と猿
なぜN+1について話すのか ・Ruby on Railsの課題で毎度N+1問題に指摘される ・指摘されるまで気づかない → 理解できていない
・each を使う時にN+1がこのままじゃ起きるよ!とレ ビューを頂いている印象 学習前のイメージ ・each文に原因があるのでは!?
テーブル
関連付け user.rb article.rb
article.titleの場合のSQL Article Load (0.5ms) SELECT "articles".* FROM "articles"
article.user.name の場合のSQL Article Load (0.5ms) SELECT "articles".* FROM "articles" 1回
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] N回(ここでは3回)
結果 each文がN+1を引き起こす原因なのではなく、 each文の中で、articleに紐づいている、userのデータを毎回取得しにいってい ることが原因
LTの準備を通して気づいたこと
自分が今つまづいている部分だけを切りとる ・FBCの課題をしていると、全体的なコード量が多いので複雑になって しまう ・自分がわからないところだけをピックアップした、最小限のアプリ ケーションを作成する
わからないを放置しない ・FBCにいると早く課題を進めた方が良いのではという気持ちががあって先 走ってしまい、一つ一つを丁寧に理解することがおろそかになっていた ・遠まりが一番の近道 ・自分の中に、質問係をつくり、質問してもらい、それに答えられるかという のをする 答え N+1はなぜ起こるの?
LTに参加ボタンを押すと良いことがある ・【RubyでつくるRuby ゼロから学びなおすプログラミング言語入門】 → 積読してあった本を読んだ! ・再帰 について調べた → 新しい知識を得られた! ・rubyでリップ(口紅)の色を調べてみた → 新たなgemについて学べた!
ご清聴 ありがとうございました!